﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;

namespace IGGWindowApp.Panels
{
    public partial class PanelBoards : UserControl
    {
        public delegate void DayChangeHandler(bool before);
        public event DayChangeHandler dayChanged;

        public delegate void BoxSelectionHandler(Game game);
        public event BoxSelectionHandler boxSelected;

        private Boxscore[] boxes = new Boxscore[15];

        public PanelBoards()
        {
            InitializeComponent();
            int startX = 3;
            int startY = 29;
            int row = 0;
            for (int i = 0; i < 15; i++)
            {
                boxes[i] = new Boxscore();
                boxes[i].Initialize();
                boxes[i].Location = new Point(startX, startY);
                boxes[i].boxSelected += new Boxscore.BoxSelectionHandler(PanelBoards_boxSelected);
                this.Controls.Add(boxes[i]);
                row++;
                startY += 88;
                if (row > 4)
                {
                    row = 0;
                    startX += 221;
                    startY = 29;
                }
            }
        }

        private void PanelBoards_boxSelected(Game game)
        {
            for (int i = 0; i < 15; i++)
            {
                if (boxes[i].boxGame != game)
                {
                    boxes[i].ResetColors();
                }
            }
            boxSelected(game);
        }

        public void SetData(Scoreboard board)
        {
            int counter = 0;
            foreach (Game game in board.GamesList.Games)
            {
                boxes[counter].UpdateData(game);
                counter++;
                if (counter > 14)
                {
                    break;
                }
            }
        }

        private class Boxscore : Panel
        {
            public delegate void BoxSelectionHandler(Game game);
            public event BoxSelectionHandler boxSelected;

            private Color HIGHLIGHT = SystemColors.ControlDarkDark;
            private Game m_boxGame = null;

            private Label labelStatus;
            private Label labelHR;
            private Label labelLine1;
            private Label labelLine2;
            private Label labelTeam2Stats;
            private Label labelTeam2Errors;
            private Label labelTeam2Hits;
            private Label labelTeam2Score;
            private Label labelTeam2Name;
            private Label labelTeam1Stats;
            private Label labelTeam1Hits;
            private Label labelTeam1Errors;
            private Label labelTeam1Score;
            private Label labelTeam1Name;
            private Panel panelHome;
            private Panel panelAway;

            public Game boxGame
            {
                get
                {
                    return m_boxGame;
                }
            }

            /// <summary>
            /// Update the boxscore fields
            /// </summary>
            public void UpdateData(Game game)
            {
                if (game == null)
                {
                    this.Visible = false;
                }
                else
                {
                    m_boxGame = game;

                    this.Visible = true;
                    labelTeam1Name.Text = game.AwayTeam.Abbrv.ToUpper();
                    labelTeam2Name.Text = game.HomeTeam.Abbrv.ToUpper();
                    labelTeam1Stats.Text = "(" + game.AwayTeam.Wins.ToString() + "-" + game.AwayTeam.Losses.ToString() + ")";
                    labelTeam2Stats.Text = "(" + game.HomeTeam.Wins.ToString() + "-" + game.HomeTeam.Losses.ToString() + ")";
                    labelTeam1Score.Text = game.AwayScore.ToString();
                    labelTeam2Score.Text = game.HomeScore.ToString();
                    if (game.Linescore != null)
                    {
                        labelTeam1Hits.Text = game.Linescore.GetAwayLineScore().Hits.ToString();
                        labelTeam2Hits.Text = game.Linescore.GetHomeLineScore().Hits.ToString();
                        labelTeam1Errors.Text = game.Linescore.GetAwayLineScore().Errors.ToString();
                        labelTeam2Errors.Text = game.Linescore.GetHomeLineScore().Errors.ToString();
                    }
                    switch (game.Status.Indicator.Category)
                    {
                        case Indicator.IndicatorCategoy.TERMINATED:
                            labelStatus.Text = "F";
                            labelLine2.Text = "W: " + game.Status.WinningPitcher.LastName
                                + " (" + game.Status.WinningPitcher.Wins
                                + "-" + game.Status.WinningPitcher.Losses
                                + ")   " + "L: " + game.Status.LosingPitcher.LastName
                                + " (" + game.Status.LosingPitcher.Wins
                                + "-" + game.Status.LosingPitcher.Losses + ")";
                            if (game.Status.SavingPitcher != null && game.Status.SavingPitcher.LastName != "")
                            {
                                labelLine1.Text = "S: " + game.Status.SavingPitcher.LastName
                                    + " (" + game.Status.SavingPitcher.Saves + ")";
                            }
                            else
                            {
                                labelLine1.Text = "";
                            }
                            labelHR.Visible = true;
                            string hrstr = "";
                            if (game.Status.HomeRuns.Count > 0)
                            {
                                hrstr = "HR: ";
                                foreach (HomeRun hr in game.Status.HomeRuns)
                                {
                                    hrstr += hr.Player.LastName + ", ";
                                }
                                hrstr = hrstr.Substring(0, hrstr.Length - 2);
                                if (game.Status.SavingPitcher != null && game.Status.SavingPitcher.LastName != "")
                                {
                                    labelHR.Text = hrstr;
                                }
                                else
                                {
                                    labelLine1.Text = hrstr;
                                    labelHR.Text = "";
                                }
                            }
                            else
                            {
                                labelHR.Text = "";
                            }

                            break;
                        case Indicator.IndicatorCategoy.INTERRUPTED:
                            labelStatus.Text = "R";
                            labelHR.Visible = false;
                            break;
                        case Indicator.IndicatorCategoy.IN_PROGRESS:
                            labelStatus.Text = game.Status.Inning.ToString();
                            labelHR.Visible = true;
                            labelLine2.Text = "Pitching: " + game.Status.CurrentPitcher.FullName;
                            labelLine1.Text = "Batting: " + game.Status.CurrentBatter.FullName;
                            if (game.Status.InningTop)
                            {
                                labelTeam1Stats.Text += " *";
                            }
                            else
                            {
                                labelTeam2Stats.Text += " *";
                            }
                            if (game.Status.HomeRuns.Count > 0)
                            {
                                hrstr = "HR: ";
                                foreach (HomeRun hr in game.Status.HomeRuns)
                                {
                                    hrstr += hr.Player.LastName + ", ";
                                }
                                hrstr = hrstr.Substring(0, hrstr.Length - 2);
                                labelHR.Text = hrstr;
                            }
                            else
                            {
                                labelHR.Text = "";
                            }
                            break;
                        case Indicator.IndicatorCategoy.NEXT:
                            labelStatus.Text = game.StartTime;
                            if (game.AwayProbable != null)
                            {
                                labelLine2.Text = game.AwayProbable.Name;
                                labelLine1.Text = game.HomeProbable.Name;
                            }
                            else
                            {
                                labelLine2.Text = "";
                                labelLine1.Text = "";
                            }
                            labelHR.Visible = false;
                            break;
                        case Indicator.IndicatorCategoy.WARMUP:
                            labelStatus.Text = game.StartTime;
                            if (game.AwayProbable != null)
                            {
                                labelLine2.Text = game.AwayProbable.Name;
                                labelLine1.Text = game.HomeProbable.Name;
                            }
                            else
                            {
                                labelLine2.Text = "";
                                labelLine1.Text = "";
                            }
                            labelHR.Visible = false;
                            break;
                    }
                }
            }

            public void Initialize()
            {
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.Size = new System.Drawing.Size(215, 82);

                labelStatus = new Label();
                labelStatus.Font = new System.Drawing.Font("Franklin Gothic Book", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelStatus.Location = new System.Drawing.Point(162, 49);
                labelStatus.Size = new System.Drawing.Size(48, 19);
                labelStatus.Text = "F";
                labelStatus.TextAlign = System.Drawing.ContentAlignment.TopRight;
                this.Controls.Add(labelStatus);

                labelHR = new Label();
                labelHR.AutoSize = true;
                labelHR.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelHR.Location = new System.Drawing.Point(1, 66);
                labelHR.Size = new System.Drawing.Size(74, 15);
                labelHR.Text = "HR:";
                this.Controls.Add(labelHR);

                labelLine1 = new Label();
                labelLine1.AutoSize = true;
                labelLine1.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelLine1.Location = new System.Drawing.Point(1, 51);
                labelLine1.Size = new System.Drawing.Size(72, 15);
                labelLine1.Text = "SV: Wilson (3)";
                this.Controls.Add(labelLine1);

                labelLine2 = new Label();
                labelLine2.AutoSize = true;
                labelLine2.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelLine2.Location = new System.Drawing.Point(1, 36);
                labelLine2.Size = new System.Drawing.Size(155, 15);
                labelLine2.Text = "W: Romero (6-3) L: Perez (3-11)";
                this.Controls.Add(labelLine2);

                panelHome = new Panel();
                panelHome.BackColor = System.Drawing.SystemColors.Control;
                panelHome.Dock = System.Windows.Forms.DockStyle.Top;
                panelHome.Location = new System.Drawing.Point(0, 20);
                panelHome.Size = new System.Drawing.Size(213, 20);
                this.Controls.Add(panelHome);

                labelTeam2Stats = new Label();
                labelTeam2Stats.AutoSize = true;
                labelTeam2Stats.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Stats.Location = new System.Drawing.Point(33, -1);
                labelTeam2Stats.Size = new System.Drawing.Size(40, 16);
                labelTeam2Stats.Text = "";
                panelHome.Controls.Add(labelTeam2Stats);

                labelTeam2Errors = new Label();
                labelTeam2Errors.AutoSize = true;
                labelTeam2Errors.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Errors.Location = new System.Drawing.Point(191, -2);
                labelTeam2Errors.Size = new System.Drawing.Size(15, 16);
                labelTeam2Errors.Text = "0";
                panelHome.Controls.Add(labelTeam2Errors);

                labelTeam2Hits = new Label();
                labelTeam2Hits.AutoSize = true;
                labelTeam2Hits.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Hits.Location = new System.Drawing.Point(170, -2);
                labelTeam2Hits.Size = new System.Drawing.Size(15, 16);
                labelTeam2Hits.Text = "0";
                panelHome.Controls.Add(labelTeam2Hits);

                labelTeam2Score = new Label();
                labelTeam2Score.AutoSize = true;
                labelTeam2Score.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Score.Location = new System.Drawing.Point(150, -2);
                labelTeam2Score.Size = new System.Drawing.Size(14, 15);
                labelTeam2Score.Text = "0";
                panelHome.Controls.Add(labelTeam2Score);

                labelTeam2Name = new Label();
                labelTeam2Name.AutoSize = true;
                labelTeam2Name.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Name.Location = new System.Drawing.Point(1, -1);
                labelTeam2Name.Size = new System.Drawing.Size(29, 15);
                labelTeam2Name.Text = "";
                panelHome.Controls.Add(labelTeam2Name);

                panelAway = new Panel();
                panelAway.BackColor = System.Drawing.SystemColors.Control;
                panelAway.Dock = System.Windows.Forms.DockStyle.Top;
                panelAway.Location = new System.Drawing.Point(0, 0);
                panelAway.Size = new System.Drawing.Size(213, 20);
                this.Controls.Add(panelAway);

                labelTeam1Stats = new Label();
                labelTeam1Stats.AutoSize = true;
                labelTeam1Stats.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Stats.Location = new System.Drawing.Point(33, 3);
                labelTeam1Stats.Size = new System.Drawing.Size(40, 16);
                labelTeam1Stats.Text = "";
                panelAway.Controls.Add(labelTeam1Stats);

                labelTeam1Hits = new Label();
                labelTeam1Hits.AutoSize = true;
                labelTeam1Hits.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Hits.Location = new System.Drawing.Point(170, 3);
                labelTeam1Hits.Size = new System.Drawing.Size(15, 16);
                labelTeam1Hits.Text = "0";
                panelAway.Controls.Add(labelTeam1Hits);

                labelTeam1Errors = new Label();
                labelTeam1Errors.AutoSize = true;
                labelTeam1Errors.Font = new System.Drawing.Font("Franklin Gothic Book", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Errors.Location = new System.Drawing.Point(191, 3);
                labelTeam1Errors.Size = new System.Drawing.Size(15, 16);
                labelTeam1Errors.Text = "0";
                panelAway.Controls.Add(labelTeam1Errors);

                labelTeam1Score = new Label();
                labelTeam1Score.AutoSize = true;
                labelTeam1Score.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Score.Location = new System.Drawing.Point(150, 3);
                labelTeam1Score.Size = new System.Drawing.Size(14, 15);
                labelTeam1Score.Text = "0";
                panelAway.Controls.Add(labelTeam1Score);

                labelTeam1Name = new Label();
                labelTeam1Name.AutoSize = true;
                labelTeam1Name.Font = new System.Drawing.Font("Franklin Gothic Book", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Name.Location = new System.Drawing.Point(1, 3);
                labelTeam1Name.Size = new System.Drawing.Size(26, 15);
                labelTeam1Name.Text = "";
                panelAway.Controls.Add(labelTeam1Name);

                this.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Name.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Score.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Errors.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Hits.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Stats.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                panelAway.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Name.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Score.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Hits.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Errors.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Stats.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                panelHome.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelLine2.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelLine1.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelHR.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelStatus.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
            }

            private void Boxscore_MouseClick(object sender, MouseEventArgs e)
            {
                this.BackColor = HIGHLIGHT;
                this.ForeColor = SystemColors.Control;
                labelTeam1Name.BackColor = HIGHLIGHT;
                labelTeam1Score.BackColor = HIGHLIGHT;
                labelTeam1Errors.BackColor = HIGHLIGHT;
                labelTeam1Hits.BackColor = HIGHLIGHT;
                labelTeam1Stats.BackColor = HIGHLIGHT;
                panelAway.BackColor = HIGHLIGHT;
                labelTeam2Name.BackColor = HIGHLIGHT;
                labelTeam2Score.BackColor = HIGHLIGHT;
                labelTeam2Hits.BackColor = HIGHLIGHT;
                labelTeam2Errors.BackColor = HIGHLIGHT;
                labelTeam2Stats.BackColor = HIGHLIGHT;
                panelHome.BackColor = HIGHLIGHT;
                labelLine2.BackColor = HIGHLIGHT;
                labelLine1.BackColor = HIGHLIGHT;
                labelHR.BackColor = HIGHLIGHT;
                labelStatus.BackColor = HIGHLIGHT;

                boxSelected(m_boxGame);
            }

            public void ResetColors()
            {
                this.BackColor = SystemColors.Control;
                this.ForeColor = Color.Black;
                labelTeam1Name.BackColor = SystemColors.Control;
                labelTeam1Score.BackColor = SystemColors.Control;
                labelTeam1Errors.BackColor = SystemColors.Control;
                labelTeam1Hits.BackColor = SystemColors.Control;
                labelTeam1Stats.BackColor = SystemColors.Control;
                panelAway.BackColor = SystemColors.Control;
                labelTeam2Name.BackColor = SystemColors.Control;
                labelTeam2Score.BackColor = SystemColors.Control;
                labelTeam2Hits.BackColor = SystemColors.Control;
                labelTeam2Errors.BackColor = SystemColors.Control;
                labelTeam2Stats.BackColor = SystemColors.Control;
                panelHome.BackColor = SystemColors.Control;
                labelLine2.BackColor = SystemColors.Control;
                labelLine1.BackColor = SystemColors.Control;
                labelHR.BackColor = SystemColors.Control;
                labelStatus.BackColor = SystemColors.Control;
            }
        }

        private void lblYesterday_MouseClick(object sender, MouseEventArgs e)
        {
            dayChanged(true);
        }

        private void lblTomorrow_MouseClick(object sender, MouseEventArgs e)
        {
            dayChanged(false);
        }

        private void PanelBoards_MouseClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < 15; i++)
            {
                boxes[i].ResetColors();
            }
            boxSelected(null);
        }
    }
}
